home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / math / fixfloa2 / fixfloat.doc < prev    next >
Encoding:
Text File  |  1995-03-26  |  25.1 KB  |  867 lines

  1.  
  2.                          
  3.                          FIX-FLOAT
  4.                         ===========
  5.  
  6.                        A LIBRARY FOR
  7.                      RAPID HANDLING OF
  8.                       DECIMAL NUMBERS
  9.                         REPRESENTED
  10.                            ON A
  11.                         FIXED POINT
  12.                       BINARY NOTATION
  13.  
  14.                              +
  15.  
  16.                     32 bit inexhaustible
  17.                             ???
  18.                         random number 
  19.                           generator
  20.                           
  21.                        
  22.                        -------------
  23.                       | Version 1.2 |
  24.                        -------------
  25.  
  26.  
  27.  
  28. REQUIREMENTS :
  29. ==============
  30.  
  31. The routines are written to be run in protected mode with 32 bit default
  32. data size.  A C - compiler is required to set up data structures, this
  33. could be done in any highlevel language that can export symbols visible
  34. to assembly language.  C++ uses a strange naming convention of their 
  35. symbols making the task more difficult.  Support of floating point 
  36. processing is also required to setup data structures. No 486 or higher 
  37. instructions are used, a 386 is required however.  All timings given
  38. in the function descriptions below are based on Intels 486.
  39.  
  40.  
  41. INTRODUCTION :
  42. ==============
  43.  
  44. Fixfloat is a small library for handling decimal numbers represented on
  45. a fixed point number format. A decimal number is represented with 32
  46. bits, using the upper 16 bits as the integral part and the lower 16
  47. bits as the decimal part :
  48.  
  49.               31    24       16        8        0  ( bit nr )
  50.               ________ ________ ________ ________
  51. register :   |________|________|________|________|
  52.                integral part   ^  decimal part
  53.  
  54. A few decimal numbers and their hexadecimal representation as fixfloats:
  55.  
  56.                 Decimal number | Fixfloat (hex)
  57.                ---------------------------------
  58.                       1/16     |  0000 1000h
  59.                       0.25     |  0000 4000h
  60.                       0.5      |  0000 8000h
  61.                       1        |  0001 0000h
  62.                       7.1875   |  0007 1000h
  63.  
  64. In C language conversion from floats & doubles to fixfloats is
  65. performed by multiplying by 65536 and taking the integral part of this
  66. result.
  67.  
  68.  
  69. ADVANTAGES & DISADVANTAGES :
  70. ============================
  71.  
  72. The advantage of this representation of decimal numbers are that
  73. calculations are quicker,  usually by a factor 1.5 to 4, compared to
  74. using floating point instructions,  the process of converting to an
  75. integer is quick & simple ( 'shr eax,16' if eax contains a fixfloat in
  76. assembler, (a>>16) in C if 'a' is a fixfloat ).  Converting from
  77. floatingpoint register to integer takes 33 clockcycles on a 486,  it
  78. takes 2 clock cycles to go from fixfloat to integer.  Another advantage
  79. is that software using this format performs equally whether there is an
  80. FPU available or not.  This remains an argument since many new x86
  81. processors of various brands ships without floating point unit.  Adding
  82. and subtracting operations use standard add & sub instructions and
  83. computes 24 times faster than the fpu equivalents.  The primary
  84. disadvantage of this format is of course that the range of the numbers
  85. is limited to -32768 to 32767.  Other disadvantages are that precision
  86. is limited and that for most operations a subroutine call is required
  87. with added time for call & ret instructions.  This can be undone by
  88. expanding the function into the calling body.  These routines are not
  89. aimed towards applications where the result is numerically presented to
  90. the user but rather where there is no need for better precision than a
  91. 4-5 decimal digits.  Another disadvantage is that multiplications are
  92. relatively slow on the integer unit compared to the floating point
  93. unit, this is true for both 486 & 586 processors.
  94.  
  95.  
  96. ALGORITHMS :
  97. ============
  98.  
  99. Divisions and multiplications are performed by unsigned integer machine
  100. instructions, using the 64 bit results of multiplication and the
  101. possibility to use 64 bits in division denominators. No precision or
  102. time is lost in this way.  The trancendental functions supported are
  103. usually performed by a number of shifts to get the in value in an
  104. appropriate range and then looking in a data table and some shifting of
  105. the result. Precision is limited by how large data tables one wants.
  106. Typically 12 bits precision of the incoming value is supported but this
  107. is easily modified.
  108.  
  109.  
  110. TRIGONOMETRY :
  111. ==============
  112.  
  113. The anglesum of a circle is 65536 instead of 2 PI,  thus a circle have
  114. angles from 0 to 1. Here are some angles on different angle bases :
  115.  
  116.         DEG  |  RAD   |  FIXFLOAT
  117.        ===========================
  118.         45   |  PI/4  |  02000h
  119.         90   |  PI/2  |  04000h
  120.         180  |  PI    |  08000h
  121.         360  |  2PI   |  10000h
  122.  
  123. Sin & cos functions have 16384 steps on a circle, the asin, acos
  124. functions goes from 0 to 1 in 4096 steps.  The tan function goes from
  125. zero to 90 degrees in 1024 steps, if angles are larger than 84.375
  126. degrees a separate 256 entry table is used and if angles are larger
  127. than 88.59375 a last 256 entry table is used.  The atan functions uses
  128. a table of 1024 entries if argument is less than 4 ( 0-4/1024 ). If
  129. argument is less than 16 it uses a table of 256 entries ( 0-16/256 ) if
  130. argument is less than 512 it uses a table of 256 entries ( 0-512/256 ),
  131. if argument is less than 8192 it uses a table of 256 entries ( 0-8192/
  132. 256).
  133.  
  134.  
  135. COMBINED ARITHMETIC FUNCTIONS :
  136. ===============================
  137.  
  138. By squaring a number larger than 256 the 16 bit integral part
  139. overflows. By combining a few actions in a function one can sometimes
  140. benefit from the 64 bit intermediate format. This is used by functions
  141. ffhyp, fftrihyp.  The operations performed by ffhyp & fftrihyp are :
  142.  
  143. ffhyp = sqrt ( a*a+b*b )  fftrihyp = sqrt ( a*a+b*b+c*c )
  144.  
  145. It is also possible to eliminate intermediate shifts if both divison
  146. and multiplication is performed.  This is used in following operations :
  147.  
  148.                 a * b                a * b
  149.   ffmuldiv  =  -------     ffmmd  = -------
  150.                 c * d                  c
  151.  
  152.  
  153. DATA TABLES :
  154. ================
  155.  
  156. The gen_fix_float_tables() function will setup the data tables needed
  157. by the trancemdental functions. A table is also setup for a random 
  158. number generator. This function assumes that the compiler environment
  159. has some sort of support for double numbers and elementary trancendental
  160. functions. A random number generator must be present also. The rand 
  161. function is expected to return a random number covering at least bits 
  162. 0-14 (15 bits). At current the data tables occupies approximately 64 kb.
  163. The random table stands for 16 of those. Only the pure mul &  div 
  164. functions can be used without intializing data tables. It seems that
  165. it takes something like a second to intialize all tables.
  166.  
  167.  
  168.  
  169. BASIC ARITHMETIC OPERATIONS,  C LANGUAGE :
  170. ==========================================
  171.  
  172. The file fixfloat.asm will have to be included in the project. In C-files
  173. using fixfloat functions the file "fixfloat.h"  should be included by
  174. '#include "fixfloat.h"' statement. If other than mult / div functions
  175. are used the file cfifloa.c must be included in the project and the
  176. function gen_fixfloat_table() must be called before using any of the
  177. transcendental functions.
  178.  
  179. Suppose we want to express 3.1415 & 2.7182 as a fixfloats in C.
  180. How do we do ?
  181.  
  182. // Assign example
  183. float e;
  184. long  a,b;
  185.   a = 3.1415 * 65536;        // The precompiler will take care of the 
  186.                              // floatingpoint operator. a is now 
  187.   e = 2.7182;                // fixfloat representation of 3.1415
  188.   b = e * 65536;             // The floating point hardware or functiom
  189.                              // or emulator will take care of multiplication.
  190.                              // b now holds fixfloat representation of 2.7182
  191.  
  192. Suppose we've been doing some operations on a fixfloat quantity and want
  193. the integer part of it :
  194.  
  195. long a, int_part;
  196.   // code
  197.   // a holds fixfloat number and we want integer part to be placed
  198.   // in int_part :
  199.   int_part = a>>16;
  200.  
  201. Addition & Subtraction:
  202.  
  203. // Addition demo
  204. long a,b,c;  // declare three 32 bit quantities
  205.   a = 8.7 * 65536;
  206.   b = 2.2 * 65536;
  207.   c = a + b;   // add & put in c ( c holding 10.9 as fixfloat quantity )
  208.  
  209.  
  210. Multiplication :
  211.  
  212. To multiply fixfloat numbers from C one can use functions or inline 
  213. assembly functions . The inline version is approx 8 cycles faster.
  214.  
  215. // Inline signed multiply demo:
  216. #include "fixfloat.h"
  217.  
  218. void main(){
  219.    long a,b,c;
  220.  
  221.    a = 3.3 * 65536;
  222.    b = -3.0 * 65536;
  223.  
  224.    c = iffsmul(a,b); // c will hold the number -9.9 in fixfloat form.
  225.    }
  226.  
  227. // Unsigned multiply demo ( calls Asm function ):
  228. #include "fixfloat.h"
  229.  
  230. void main(){
  231.    long a,b,c;
  232.  
  233.    a = 3.3 * 65536;
  234.    b = 1000.0 * 65536;
  235.  
  236.    c = ffmul(a,b); // c will hold the number 3300 in fixfloat form.
  237.    }
  238.  
  239. To help renember name of functions, let's decode 'iffsmul' :
  240. i   - stands for inline assmebly version,
  241. ff  - stands for fixfloat,
  242. s   - stands for signed ( can use negative numbers )
  243. mul - stands for multiplication
  244.  
  245.  
  246. HINT : To have faster multiplications, if you know which value is 
  247.        the lower one, pass it as the 2nd argument, since  
  248.        multiplication happens bitwise and stops when all remaining 
  249.        bits are 0.
  250.  
  251. Division :
  252.  
  253. To divide fixfloat numbers from C one can use functions or inline 
  254. assembly functions . The function version (ffdiv, ffsdiv) checks 
  255. it's arguments to see if a divide overflow would occur, in this 
  256. case the biggest possible number is returned, otherwise the division
  257. is performed. The inline versions does no argument checking and might
  258. raise exceptions.
  259.  
  260. // Inline signed divide demo:
  261. #include "fixfloat.h"
  262.  
  263. void main(){
  264.    long a,b,c;
  265.  
  266.    a = 15.5 * 65536;
  267.    b = -5.0 * 65536;
  268.  
  269.    c = iffsdiv(a,b); // c will hold the number -3.1 in fixfloat form.
  270.    }
  271.  
  272. // Unsigned divide demo ( calls Asm function ):
  273. #include "fixfloat.h"
  274.  
  275. void main(){
  276.    long a,b,c;
  277.  
  278.    a = 10.0 * 65536;
  279.    b = 4.0 * 65536;
  280.  
  281.    c = ffdiv(a,b); // c will hold the number 2.5 in fixfloat form.
  282.    }
  283.  
  284.  
  285. BASIC ARITHMETIC OPERATIONS,  ASM LANGUAGE :
  286. ============================================
  287.  
  288. The file fixfloat.inc will have to be included in the .asm files using
  289. fixfloat functions. In your project 'fixfloat.asm' must be present.
  290. Fixfloats use 32 bits to represent themselves so full registers (EAX, 
  291. EBX ...) should be used. Any 4 byte memory location stores them. The C-
  292. function gen_fixfloat_tables must be called before using other than 
  293. mult / div functions.
  294.  
  295. Addition & subtraction :
  296.  
  297. Addition & subtraction works with std x86 add & sub operators:
  298.  
  299. ; Addition demo
  300.  
  301. mov        eax,8.7 * 65536
  302. mov        ebx,2.2 * 65536
  303. add        ebx,eax           ; ebx will now hold 10.9 in fixfloat notation
  304.  
  305.  
  306. Multiplication :
  307.  
  308. There are two ways to multiply fixfloats from within asm. One can either
  309. call a function to do it or use a macro. The macro version will be 
  310. typically 8 clocks faster.
  311.  
  312. ; Macro signed multiply example :
  313.  
  314. mov        eax,3.3 * 65536
  315. mov        ebx,-3.0 * 65536
  316. MFFSMUL    ebx              ; eax will now hold -9.9 in fixfloat form.
  317. ; NOTE edx will be modified by this macro !!!
  318.  
  319.  
  320. ; Unsigned multiply by function call example
  321.  
  322. mov        eax,3.3 * 65536
  323. mov        edx,1000.0 * 65536
  324. call       _ffmul           ; eax will now hold 3300.0 in fixfloat form.
  325.  
  326. ; NOTE edx will be modified by this function !
  327.  
  328.  
  329. Division :
  330.  
  331. There are two ways to divide fixfloats from within asm. One can either
  332. call a function to do it or use a macro. The function version (_ffdiv,
  333. _ffsdiv) checks it's arguments to see if a divide overflow would occur, 
  334. in this case the biggest possible number is returned, otherwise the 
  335. division is performed. The inline versions does no argument checking 
  336. and might raise exceptions.
  337.  
  338. ; Macro signed division example :
  339.  
  340. mov        eax,15.5 * 65536
  341. mov        ebx,-5 * 65536
  342. MFFSDIV    ebx                 ; eax will now hold -3.1 in fixfloat form
  343. ; NOTE edx will be modified by this macro !!!
  344.  
  345. ; unsigned divide by function call example :
  346.  
  347. mov        eax,10.0 * 65536
  348. mov        ebx,4.0 * 65536
  349. call       _ffdiv           ; eax will contain 2.5 in fixfloat form.  
  350. ; NOTE edx will be modified by this function !!!
  351.  
  352.  
  353. GENERAL INFO ON FUNCTIONS :
  354. ===========================
  355.  
  356. I have tried clocking a few of the functions using a hires timer but 
  357. the results are inconsistent and I don't want to put too much effort 
  358. into it.  Most instructions in the funtions execute in one clock, 
  359. except for shifts (2-4), multiplications (13-42), divisions (40), 16
  360. bits loads (2).  This is true for a 486 processor,  for a 586 the 
  361. situation is different again. AGI stalls have been avoided to a large 
  362. extent. I have added time for loading data from math tables since this
  363. data at most times won't be in the primary cache.  For a 16 bit load
  364. ( which is frequently usde to lessen table size ) with a 'eax*2' offset
  365. I have counted 7 clocks.
  366.  
  367. None of the functions use any local or global variables, a few uses some 
  368. stackspace (max 32 bytes).  The timings mentioned below are based on 
  369. instruction count and do not include the call and the ret statement.
  370.  
  371. If called from C the names below should be used, if called from within 
  372. assembly language a underbar must be added to end of functionname 
  373. ( Watcom Std ). Parameters to the functions are used in the following
  374. order :
  375.   
  376.   eax - first argument
  377.   edx - second argument
  378.   ebx - third argument
  379.   ecx - fourth argument
  380.  
  381. Return values are in eax for all functions.
  382.  
  383. For inline usage of multiply & divide, see section BASIC OPERATIONS,
  384. C LANGUAGE.
  385.  
  386. For MACRO usage of multiply & divide, see section BASIC OPERATIONS,
  387. ASM LANGUAGE.
  388.  
  389.  
  390.  
  391. DESCRIPTION OF FIXFLOAT FUNCTIONS :
  392. ===================================
  393.  
  394.  
  395. gen_fixfloat_tables :
  396. ---------------------
  397.  
  398. Will calculate all the tables needed by below functions, should be called
  399. when using other first of all when using other functions than pure mul
  400. and div functions.
  401.  
  402. Clocks  : approx20 000 000
  403.  
  404.  
  405. fftoa(long ff, char *buf) :
  406. ---------------------------
  407.  
  408. Converts fixfloat ff to ascii decimal representation, puts result
  409. where buf points. Zero truncated format, 3.25 represented by '3.25'.
  410.  
  411.  
  412. fftoah(long ff, char *buf) :
  413. ---------------------------
  414.  
  415. Converts fixfloat ff to ascii hexadecimal representation, puts result
  416. where buf points. Full format, 3.25 represented by '0003.4000'.
  417.  
  418.  
  419. ffsmul :
  420. --------
  421. Multiplies the fixfloat in eax by the fixfloat in edx. Result in eax.
  422. The result has sign according to the factors.
  423. This function consists of only 2 instructions and is therefore good
  424. to expand into calling body : 
  425.  
  426.   imul    edx          ; these two instructions does the job
  427.   shrd    eax,edx,16
  428.  
  429. Affects : edx
  430. Clocks  : 20 - 45
  431.  
  432.  
  433. ffsdiv :
  434. --------
  435. Divides the fixfloat in eax by the fixfloat in edx. Result in eax. 
  436. Guards against the situation where the result doesn't fit into 32
  437. bits. max positive number or max negative number is then returned.
  438. Thus the function cannot generate any division by 0 error. 0/0 will
  439. also return 07FFF.FFFF. The result has sign according to the 
  440. nominator & denominator.
  441.  
  442. Affects : none
  443. Clocks  : 73
  444.  
  445. NOTE : When certain that the result will fit into 32 bits use iffsdiv
  446.        in from C or macro MFFSDIV from Asm. This will gain approx 20
  447.        cycles.
  448.  
  449. NOTE : Divisions are quite a bit slower than multiplications, if
  450.        performing many divisions by the same number x it might be 
  451.        good to divide 1 by x and then multiply by this.
  452.  
  453.  
  454. ffmul :
  455. -------
  456. Unsigned multiplication by two fixfloats, eax and edx. Result in eax. 
  457. This function consists of only 2 instructions and is therefore good
  458. to expand into calling body :
  459.  
  460.   mul     edx          ; these two instructions does the job
  461.   shrd    eax,edx,16
  462.  
  463. Affects : edx
  464. Clocks  : 20 - 45
  465.  
  466.  
  467. ffdiv :
  468. -------
  469. Unsigned division between two fixfloats, eax and edx, result in eax.
  470. Checking as ffsdiv.
  471.  
  472. Affects : none
  473. Clocks  : 50
  474.  
  475. NOTE When certain that the result will fit into 32 bits use iffdiv
  476.      in from C or macro MFFDIV from Asm.
  477.  
  478.  
  479. ffsin :
  480. -------
  481. eax contains angle to take sinus from. Value returned in eax.
  482.  
  483. Affects : none
  484. Clocks  : 26
  485.  
  486.  
  487. ffcos :
  488. -------
  489. eax contains angle to take cosinus from. Value returned in eax.
  490.  
  491. Affects : none
  492. Clocks  : 27
  493.  
  494.  
  495. ffasin :
  496. -------
  497. eax contains angle to take arcsinus from. Value returned in eax.
  498.  
  499. Affects : none
  500. Clocks  : 21
  501.  
  502.  
  503. ffacos :
  504. -------
  505. eax contains angle to take arccosinus from. Value returned in eax.
  506.  
  507. Affects : none
  508. Clocks  : 23
  509.  
  510.  
  511. fftan :
  512. -------
  513. eax contains angle to take tan from. Value returned in eax.
  514.  
  515. Affects : none
  516. Clocks  : 23 - 33
  517.  
  518.  
  519. ffatan :
  520. -------
  521. eax contains angle to make angle from. Value returned in eax.
  522.  
  523. Affects : none
  524. Clocks  : 25 - 38
  525.  
  526. ff_vec_to_ang :
  527. --------------
  528. Expresses the direction of a vector in fixfloat degrees. eax
  529. contains the x component of the vector and edx the y component
  530. of the vector. A result of zero indicates a vector pointing 
  531. to the left, a result of 16384 indicates a vector pointing 
  532. straight up, a result of 32768 indicates a vector pointing
  533. to the right. The function has a resolution of 8192 steps around
  534. the unit circle.
  535.  
  536. eax - x component of vector
  537. edx - y component of vector
  538.  
  539. returns angle in eax
  540.  
  541. Affects : none
  542. Clocks  : approx 150
  543.  
  544.  
  545. fflog2 :
  546. --------
  547. eax contains fixfloat to tak 2logarithm on. Value returned in eax. 
  548.  
  549. Affects : none
  550. Clocks  : 56
  551.  
  552.  
  553. ffexp2 :
  554. --------
  555. eax contains the number to raise 2 to. Value returned in eax.
  556.  
  557. Affects : none
  558. Clocks  : 20
  559.  
  560.  
  561. ffpow :
  562. -------
  563. eax contains base and edx contains exponent. Value returned in eax.
  564. This function is a combination of fflog2, ffsmul, ffexp2.
  565.  
  566.  
  567. Affects : none
  568. Clocks  : 152
  569.  
  570.  
  571. ffsqrt:
  572. -------
  573. eax contains value to take square root of, returns squareroot in eax.
  574.  
  575. Affects : none
  576. Clocks  : 61
  577.  
  578.  
  579. ffhyp :
  580. ------- 
  581. Used to calculate hypothenuse of triangle. eax contains one side, edx
  582. the other one. Result is returned in eax. An intermediate result 
  583. larger than 32 bits will not overflow, this is the advantage compared
  584. to performing the operation step by step. 
  585.  
  586. returns sqrt ( a*a + b*b)
  587.  
  588. Affects : none
  589. Clocks  : 168
  590.  
  591.  
  592. ffalmosthyp:
  593. ------------
  594. Used to calculate an approximation to the hypothenuse of a triangle.
  595. eax contains the length of one side, edx the other. If given a negative
  596. argument it makes it positive before using. It works by adding half of 
  597. the smaller value to the bugger and returning it. To be such a simple
  598. approximation it gives a surprisingly good result. 
  599.  
  600. Affects : none
  601. Clocks  : 18
  602.  
  603.  
  604. fftrihyp :
  605. ----------
  606. As above but takes three arguments, the third is squared and added to 
  607. the two first ones. 
  608.  
  609. returns sqrt ( a*a + b*b + c*c)
  610.  
  611. Affects : none
  612. Clocks  : 212
  613.  
  614.  
  615. ffmuldiv :
  616. ----------
  617. Given four fixfloats a,b,c,d in register eax,ebx,ecx,edx it will
  618. calculate (a * d) / (b*c) . This guards agains intermediate over-
  619. flows and saves a bit of time. Returns +/- infinity if 0 in 
  620. denominator.
  621.  
  622. Affects : none
  623. Clocks  : 150
  624.             
  625.  
  626. ffmmd :
  627. -------
  628. Given three fixfloats in eax,edx,ebx it will calculate (eax*edx)/ebx
  629. allowing for more than 32 bit intermediate result.  Returns +/- infinity
  630. if 0 in denominator.
  631.  
  632. Affects : none
  633. Clocks  : 94
  634.  
  635.  
  636. ffortproj :
  637. -----------
  638.  
  639. The function is short for 'ortogonal projection', it is used for finding
  640. out how much a vector travels in another vectors direction. If it is
  641. perpendicular to the other vector 0 is returned, if it travels the same
  642. distance in the other vectors direction as the length of the other 
  643. vector 1 is returned. Negative meaning it goes in opposite direction 
  644. and a number above one that it goes further than the other vector in
  645. its direction. 
  646.  
  647. This function uses shifting (5 steps right ) to keep result within
  648. format. Be aware !
  649.  
  650. Vector (b,d) is projected onto (a,c) :
  651.  
  652.  eax - a      ( two vectors (a,c) , (b,d) )    
  653.  edx - b
  654.  ebx - c
  655.  ecx - d
  656.  
  657.  returns (a*b+c*d)/(a*a+c*c)
  658.  
  659. Affects : none
  660. Clocks  : A lot :)
  661.  
  662.  
  663. ffortproj1 :
  664. -----------
  665.  
  666. Same as above function but it takes square root of denominator before
  667. dividing, therefore it returns the length travelled in the other vectors
  668. direction. This is what is usually meant by ortogonal projection.
  669.  
  670.  
  671. This function uses shifting (5 steps right ) to keep result within
  672. format. Be aware !
  673.  
  674. Vector (b,d) is projected onto (a,c): 
  675.  
  676.  eax - a      ( two vectors (a,c) , (b,d) )    
  677.  edx - b
  678.  ebx - c
  679.  ecx - d
  680.  
  681.  returns (a*b+c*d)/sqrt(a*a+c*c)
  682.  
  683. Affects : none
  684. Clocks  : Uncountable ...
  685.  
  686.  
  687. ffdot_through_hyps :
  688. --------------------
  689.  
  690. Function does what the name says, having two vectors (a,c) & (b,d) it
  691. returns the dot product of the vectors through the product of the 
  692. lengths of the vectors.
  693.  
  694. This function uses shifting (5 steps right ) to keep result within
  695. format. Be aware !
  696.  
  697.  eax - a          
  698.  edx - b
  699.  ebx - c
  700.  ecx - d
  701.  
  702.  returns (a*b+c*d)/(sqrt(a*a+c*c)*sqrt(b*b+d*d))
  703.  
  704. Affects : none
  705. Clocks  : Many ...
  706.  
  707.  
  708. ff_solve_2nd_poly :
  709. -------------------
  710.  
  711. Functions finds the real roots of a 2:nd degree equation in the
  712. form :
  713.  
  714.   2
  715.  x  + px + q = 0 
  716.  
  717. Arguments :
  718.  
  719.  eax - p
  720.  edx - q
  721.  ebx - pointer to long (conj ptr)
  722.  ecx - pointer to long (pre_ptr)
  723.  
  724. returns : 
  725.  
  726.  0       - success
  727.  nonzero - couldn't find any real roots
  728.  
  729. The roots of the polynomial will be :
  730.   the contents of the pre_ptr +/- contents of conj_ptr
  731.  
  732. Example :
  733.  
  734. long p, q, conj, pre;
  735. long root1, root2;
  736.  
  737.   p = 2 * 65536;
  738.   q = -3 * 65536;
  739.   ff_solve_2nd_poly(p, q, &conj, &pre);
  740.  
  741.   root1 = pre + conj;  // root 1  -  1.0 * 65536
  742.   root2 = pre - conj;  // root 2  -  -3.0 * 65536
  743.  
  744. NOTE  When solving a square of p is used, no overflows will
  745.       appear since the function uses 64 bit intermediate results.
  746.  
  747. Affects : none
  748. Clocks  : Approx 140
  749.  
  750.  
  751. DESCRIPTION OF INTEGER FUNCTIONS :
  752. ==================================
  753.  
  754. There is only one integer function ( taking integer as result, giving
  755. integer as result supported.) More may be added.
  756.  
  757.  
  758. isqrt :
  759. -------
  760.  
  761. Function returns a true square root of its arguement. It uses the same
  762. table as ffsqrt, therefore needs gen_fixfloat_tables init to work 
  763. properly. A square root is always less than or equal to the actual square
  764. root, isqrt(15) = 3, isqrt(16) = 4 etc. Observe that precision of this
  765. one is not more than 3 digits.
  766.  
  767. Affects : none
  768. Clocks  : 58
  769.  
  770.  
  771.  
  772. DESCRIPTION OF RANDOM NUMBER FUNCTIONS :
  773. ========================================
  774.  
  775. The function rand32 is responsible for producing pseudo random numbers.
  776. The numbers are produced using a combination of a multiplication and a
  777. data table containing pregenerated numbers.  8 bits of the data table 
  778. is used for each number,  it takes 16384 times to step through it.  The
  779. multiplicand is constant during one such run, then it is changed. The
  780. random table is also mutaded at this time.  The number generated is 
  781. always dependent on the previous one.  Even if the same multiplicand 
  782. reappears when restarting on a table it is highly unlikely that the
  783. product in that moment would be the same as the one 256 million times 
  784. ago.  If this would happen the same random numbers would only appear
  785. until a mutated entrance would occur,  which would be the first to come.
  786. The period for the randomgenerator shold be : 
  787.  
  788.      16384 * 16384 * 2^32 * 2^(16384*32)  = 2 ^ 524438 
  789.  
  790. Which I think is enough for many applications. ( A DEC Alpha 330 MHz
  791. would produce 2^60 pseudo random numbers in 1000 years if dedicated to 
  792. it. )
  793.  
  794.  
  795. There are 3 memory locations visible affecting the generator :
  796.  
  797. _rnd3 - contains the number by which to multiply the last random 
  798.         number. Before multiplication a constant is added and then
  799.         the value is truncated to 12 bits to keep multiplication time
  800.         down.
  801.  
  802. _rnd2 - contains how many numbers generated so far.  Every time it 
  803.         ticks over a 16 kb limit the _rnd3 is changed.
  804.  
  805. _rnd1 - contains the last number generated.
  806.  
  807. Any of these can be changed to produce 'randomize' affect. From a given
  808. start it will always produce identical numbers.
  809.  
  810.  
  811. rand32 :
  812. --------
  813. Returns a 32 bit pseudo random number.
  814.  
  815. Affects : none
  816. Clocks  : 41
  817.  
  818.  
  819. randinter :
  820. -----------
  821. eax contains the number above the number desired. This functions 
  822. computes the 'and' value before calling rand32 and is a bit slower.
  823.  
  824. Example :
  825.    // Want random number 0-19
  826.    long rn;
  827.    rn = randinter(20);
  828.  
  829. Affects : none
  830. Clocks  : 77 - 124 ( Average c:a 110 ) 
  831.  
  832.  
  833. randpowint :
  834. ------------
  835. eax contains a number to 'and' the 32 bit random number with, edx 
  836. contains the number above the maximum number desired.  This function 
  837. can be used when the interval of the randomnumber one want's is the
  838. same from time to time, saves something like 25 clocks. If an interval
  839. close to the 'and' value in eax is chosen it will have to loop back
  840. fewer times. 
  841.  
  842. Example :
  843.  
  844.   // Want random number 0-99
  845.   long rn;
  846.   rn = randpowint(127,100)
  847.  
  848.   ....
  849.  
  850.   // Want random number 0-2999
  851.   long rn;
  852.   rn = randpowint(4095,3000);
  853.   
  854.  
  855. Affects : none
  856. Clocks  : 47 - 94 ( Average c:a 80 )
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.